
package ch.hearc.securiteSysteme.serie2.ex2ChallengeResponse.client.swing;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;

import ch.hearc.securiteSysteme.serie2.ex2ChallengeResponse.client.socket.ClientAdapter;
import ch.hearc.securiteSysteme.serie2.ex2ChallengeResponse.client.socket.SocketClient;

public class JFrameBorderLayout extends JFrameBase
	{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 8024399893793119384L;
	
	/*------------------------------------------------------------------*\
	|*							Constructeurs							*|
	\*------------------------------------------------------------------*/
	public enum STEP
		{
		BEGINNING, SENTUSERNAME
		}
	
	private JLabel labelUser;
	private JLabel labelChallenge;
	private JLabel boutonNord;
	
	private JTextField textUser;
	private JTextField textMDP;
	
	private JButton sendLogin;
	private JButton sendCle;
	
	private Box box;
	private SocketClient sc;
	
	private String nonce;
	private STEP step;
	
	public JFrameBorderLayout(SocketClient sc)
		{
		this.sc = sc;
		this.step = STEP.BEGINNING;
		
		createComponents();
		addComponents();
		addListeners();
		setPropriete();
		}
	
	/*------------------------------------------------------------------*\
	|*							Methodes Public							*|
	\*------------------------------------------------------------------*/
	
	/*------------------------------------------------------------------*\
	|*							Methodes Private						*|
	\*------------------------------------------------------------------*/
	
	private void addListeners()
		{
		this.sc.addClientListener(new ClientAdapter()
			{
				
				@Override
				public void slotMessageReceived(Object obj)
					{
					super.slotMessageReceived(obj);
					if (STEP.BEGINNING == step)
						{
						if (obj instanceof String)
							{
							JFrameBorderLayout.this.step = STEP.SENTUSERNAME;
							
							JFrameBorderLayout.this.nonce = (String)obj;
							JFrameBorderLayout.this.sendLogin.setEnabled(false);
							JFrameBorderLayout.this.sendCle.setEnabled(true);
							}
						}
					else if (STEP.SENTUSERNAME == step)
						{
						if (obj instanceof Boolean)
							{
							JFrameBorderLayout.this.step = STEP.BEGINNING;
							
							System.out.println("Le mot de passe envoye est-il correct ? " + (boolean)obj);
							JFrameBorderLayout.this.sendLogin.setEnabled(true);
							JFrameBorderLayout.this.sendCle.setEnabled(false);
							}
						}
					}
			});
		
		this.sendLogin.addActionListener(new ActionListener()
			{
				
				@Override
				public void actionPerformed(ActionEvent arg0)
					{
					String textUser = JFrameBorderLayout.this.textUser.getText();
					if (textUser != null)
						{
						try
							{
							JFrameBorderLayout.this.sc.send(textUser);
							}
						catch (IOException e)
							{
							e.printStackTrace();
							}
						}
					}
			});
		
		this.sendCle.addActionListener(new ActionListener()
			{
				
				@Override
				public void actionPerformed(ActionEvent arg0)
					{
					String textMDP = JFrameBorderLayout.this.textMDP.getText();
					if (textMDP != null)
						{
						String data = "" + textMDP + JFrameBorderLayout.this.nonce;
						
						MessageDigest mda;
						try
							{
							mda = MessageDigest.getInstance("SHA-512");
							byte[] digesta = mda.digest(data.getBytes());
							Byte[] bytearray = new Byte[digesta.length];
							
							for(int i = 0; i < digesta.length; i++)
								{
								bytearray[i] = digesta[i];
								}
							
							StringBuffer sb = new StringBuffer();
							
							for(int i = 0; i < bytearray.length; i++)
								{
								sb.append(Integer.toHexString(0xFF & bytearray[i]));
								}
							System.out.println(sb);
							JFrameBorderLayout.this.sc.send(bytearray);
							
							}
						catch (NoSuchAlgorithmException e)
							{
							e.printStackTrace();
							}
						catch (IOException e)
							{
							e.printStackTrace();
							}
						}
					}
			});
		}
	
	private void createComponents()
		{
		this.boutonNord = new JLabel("Exercice2");
		this.labelUser = new JLabel("User : ");
		this.labelChallenge = new JLabel("MDP : ");
		
		this.textUser = new JTextField();
		this.textMDP = new JTextField();
		
		this.sendLogin = new JButton("Ask Challenge");
		this.sendCle = new JButton("Send Hashage");
		
		this.box = Box.createVerticalBox();
		
		Box boxH1 = Box.createHorizontalBox();
		boxH1.add(this.labelUser);
		boxH1.add(this.textUser);
		boxH1.add(this.sendLogin);
		
		Box boxH3 = Box.createHorizontalBox();
		boxH3.add(this.labelChallenge);
		boxH3.add(this.textMDP);
		boxH3.add(this.sendCle);
		
		this.box.add(boxH1);
		this.box.add(boxH3);
		}
	
	private void addComponents()
		{
		BorderLayout borderLayout = new BorderLayout();
		borderLayout.setHgap(50);
		borderLayout.setVgap(25);
		setLayout(borderLayout);
		
		add(this.box, BorderLayout.CENTER);
		add(this.boutonNord, BorderLayout.NORTH);
		}
	
	/*------------------------------------------------------------------*\
	|*							Attributs Private						*|
	\*------------------------------------------------------------------*/
	}
